package com.example.oracle.system.config;

import com.example.oracle.pojo.system.ResponseMap;
import com.example.oracle.pojo.system.WebCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Siaze
 * @date 2021/11/8
 */
@Component
public class NoLogin implements AuthenticationEntryPoint {
    /**
     * 没登录的情况
     * @param request 请求对象
     * @param response 返回对象
     * @param authException 认证异常
     * @throws IOException IO异常
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        ResponseMap responseMap = new ResponseMap(WebCode.NOT_LOGIN);
        PrintWriter out = response.getWriter();
        out.println(responseMap);
        out.flush();
        out.close();
    }
}